from abc import abstractmethod

from PyQt5.QtWidgets import QWidget, QPushButton, QHBoxLayout, QTableWidgetItem, QHeaderView
from PyQt5.uic import loadUi

from dao.rapid_crawler_bin_db import RapidCrawlerBinManager


class WithButtonTableWidget(object):
    def __init__(self, parent_self, tableWidget, data=None, header=None, show=None, resizeMode=QHeaderView.Interactive):
        super().__init__()
        self.updateUi = None
        self.tableWidget = tableWidget
        self.data = data  # 数组形式的元组 [(),()]
        self.header = header
        self.show = show
        self.parent = parent_self
        self.resizeMode = resizeMode
        self.initUI()

    def initUI(self):
        self.tableWidget.setRowCount(len(self.data))
        self.tableWidget.setColumnCount(len(self.header))
        self.tableWidget.setHorizontalHeaderLabels(self.header)  # 需要放在设置行后
        self.tableWidget.horizontalHeader().setSectionResizeMode(self.resizeMode)
        for row_number, row_data in enumerate(self.data):
            for i in range(len(row_data) + 1):
                if i < len(row_data):
                    self.tableWidget.setItem(row_number, i, QTableWidgetItem(str(row_data[i])))
                # 添加按钮
                if i == len(row_data):
                    # 传入当前id
                    if self.tableWidget.objectName() == "crawlerBinTableWidget":
                        self.tableWidget.setCellWidget(row_number, i, self.buttonForRow(str(row_data[1]), self.show))
                    else:
                        self.tableWidget.setCellWidget(row_number, i, self.buttonForRow(str(row_data[0]), self.show))

    def buttonForRow(self, index, show):
        # 0 无权限 修改 1 查看 2 删除 4
        widget = QWidget()
        hLayout = QHBoxLayout()
        if show == 7 or show == 1 or show == 3 or show == 5:
            # 修改
            updateBtn = QPushButton('修改')
            updateBtn.setStyleSheet(''' text-align : center;
                                                  background-color : NavajoWhite;
                                                  height : 30px;
                                                  border-style: outset;
                                                  font : 13px  ''')

            updateBtn.clicked.connect(lambda: self.updateTable(index))
            hLayout.addWidget(updateBtn)
        if show == 7 or show == 2 or show == 3 or show == 6:
            # 查看
            viewBtn = QPushButton('查看')
            viewBtn.setStyleSheet(''' text-align : center;
                                          background-color : DarkSeaGreen;
                                          height : 30px;
                                          border-style: outset;
                                          font : 13px; ''')

            viewBtn.clicked.connect(lambda: self.viewTable(index))
            hLayout.addWidget(viewBtn)
        if show == 7 or show == 4 or show == 5 or show == 6:
            # 删除
            deleteBtn = QPushButton('删除')
            deleteBtn.setStyleSheet(''' text-align : center;
                                            background-color : LightCoral;
                                            height : 30px;
                                            border-style: outset;
                                            font : 13px; ''')

            deleteBtn.clicked.connect(lambda: self.deleteTable(index))
            hLayout.addWidget(deleteBtn)

        hLayout.setContentsMargins(5, 2, 5, 2)
        widget.setLayout(hLayout)
        return widget

    def updateTable(self, index):
        if self.tableWidget.objectName() == "crawlerBinTableWidget":
            print(index)
            rapid = RapidCrawlerBinManager()
            info = rapid.selectInfoByCrawlerBin(index)
            self.updateUi = loadUi('update.ui', baseinstance=None)
            self.updateUi.showbinLabel.setText(info[0][2])
            self.updateUi.remarkTextEdit.setText(info[0][8])
            # 创建一个自定义槽函数，用于处理确认按钮的点击事件
            self.updateUi.boardButtonBox.accepted.connect(self.toSaveAccount)
            self.updateUi.exec_()

    def toSaveAccount(self):
        if self.updateUi is not None:
            crawler = RapidCrawlerBinManager()
            crawler.updateRemark(
                self.updateUi.showbinLabel.text(),
                self.updateUi.remarkTextEdit.toPlainText(),
            )
            self.parent.crawler_bin_show_page(page_num=1)

    @abstractmethod
    def viewTable(self, index):
        pass

    @abstractmethod
    def deleteTable(self, index):
        if self.tableWidget.objectName() == "crawlerBinTableWidget":
            rapid = RapidCrawlerBinManager()
            rapid.batchDelByCrawlerBins([index])
            self.parent.crawler_bin_show_page(page_num=1)


